package com.sskj.bibi.ui.activity;


import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.hey.lib.HeySpinner;
import com.sskj.bibi.R;
import com.sskj.bibi.R2;
import com.sskj.bibi.bean.rxbus.AllEntrustBusBean;
import com.sskj.bibi.bean.rxbus.HistoryEntrustBusBean;
import com.sskj.bibi.presenter.RecordActivityPresenter;
import com.sskj.common.adapter.MyFragmentPagerAdapter;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import io.reactivex.Flowable;

@Route(path = RConfig.BIBI_RECORD)//订单记录
public class RecordActivity extends BaseActivity<RecordActivityPresenter> {
    @BindView(R2.id.viewPager)
    ViewPager viewPager;
    @BindView(R2.id.coinTypeSpinner)
    HeySpinner leftSpinner;
    @BindView(R2.id.tradeTypeSpinner)
    HeySpinner rightSpinner;
    @BindView(R2.id.tvFilter)
    TextView tvFilter;
    @BindView(R2.id.ivFilter)
    ImageView ivFilter;
    @BindView(R2.id.tv_history)
    TextView tvHistory;
    @BindView(R2.id.tv_all)
    TextView tvAll;
    @BindView(R2.id.btCancel)
    TextView btCancel;
    @BindView(R2.id.btInvoke)
    TextView btInvoke;
    @BindView(R2.id.btConfirm)
    TextView btConfirm;
    @BindView(R2.id.btDeal)
    TextView btDeal;
    @BindView(R2.id.llFilter)
    LinearLayout llFilter;
    @BindView(R2.id.view)
    View view;
    @BindView(R2.id.slidingTabLayout)
    SegmentTabLayout layout;
    private List<Fragment> fragments;
    private List<String> rightCodes;
    private List<String> leftCodes;
    private String currentRightCode;
    private Map<String, List<String>> coinMap;
    private String type = null;

    String[] data = null;

    @Override
    protected int getLayoutId() {
        return R.layout.bibi_activity_record;
    }

    @Override
    public RecordActivityPresenter getPresenter() {
        return new RecordActivityPresenter();
    }

    @Override
    protected void initView() {
        data = new String[]{App.INSTANCE.getString(R.string.bibirecordActivity1), App.INSTANCE.getString(R.string.bibirecordActivity2)};
        fragments = new ArrayList<>();
        fragments.add((Fragment) ARouter.getInstance().build(RConfig.BIBI_All_ENTRUST).navigation());
        fragments.add((Fragment) ARouter.getInstance().build(RConfig.BIBI_HISTORY_ENTRUST).navigation());
        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragments));

        layout.setTabData(data);
        layout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);
                llFilter.setVisibility(View.GONE);
            }

            @Override
            public void onTabReselect(int position) {

            }

        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                layout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        ClickUtil.click(tvHistory, () -> {
            tvHistory.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.libTextBlack));
            tvAll.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.libTextGray));
            viewPager.setCurrentItem(1);
            llFilter.setVisibility(View.GONE);
        });
        ClickUtil.click(tvAll, () -> {
            tvAll.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.libTextBlack));
            tvHistory.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.libTextGray));
            viewPager.setCurrentItem(0);
            llFilter.setVisibility(View.GONE);
        });
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                if (position == 0) {
                    tvAll.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.libTextBlack));
                    tvHistory.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.libTextGray));
                } else {
                    tvHistory.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.libTextBlack));
                    tvAll.setTextColor(ContextCompat.getColor(App.INSTANCE, R.color.libTextGray));
                }
            }
        });
    }

    @Override
    protected void initData() {
        mPresenter.getAllCoin();
    }

    public void updateCoinAll(Map<String, List<String>> data) {
        coinMap = data;
        Flowable.fromIterable(data.keySet())
                .toList()
                .subscribe((strings, throwable) -> {
                    strings.add(0, App.INSTANCE.getString(R.string.bibirecordActivity3));
                    rightCodes = strings;
                    rightSpinner.setItemGravity(Gravity.CENTER);
                    rightSpinner.attachData(strings);
                    currentRightCode = null;
                    changeLeftSpinnerCode();
                    rightSpinner.setOnSelectListener(position -> {
                        if (position == 0) {
                            currentRightCode = null;
                        } else {
                            currentRightCode = strings.get(position);
                        }
                        changeLeftSpinnerCode();
                    });
                });
        ClickUtil.click(tvFilter, () -> {
            if (llFilter.getVisibility() == View.VISIBLE) {
                llFilter.setVisibility(View.GONE);
            } else {
                llFilter.setVisibility(View.VISIBLE);
                type = null;
                btDeal.setTextColor(getResources().getColor(R.color.lib_dominant));
                btInvoke.setTextColor(getResources().getColor(R.color.libTextGray));
                btDeal.setBackgroundResource(R.drawable.bibi_shape);
                btInvoke.setBackgroundResource(R.drawable.bibi_shape1);
                if (viewPager.getCurrentItem() == 0) {
                    btDeal.setText(App.INSTANCE.getString(R.string.bibitipBibiUtil1));
                    btInvoke.setText(App.INSTANCE.getString(R.string.bibirecordActivity5));
                } else {
                    btDeal.setText(App.INSTANCE.getString(R.string.bibirecordActivity6));
                    btInvoke.setText(App.INSTANCE.getString(R.string.bibirecordActivity7));
                }
                if (leftSpinner != null) {
                    leftSpinner.setSelectIndex(0);
                    rightSpinner.setSelectIndex(0);
                }
            }
        });
        ClickUtil.click(ivFilter, () -> {
            if (llFilter.getVisibility() == View.VISIBLE) {
                llFilter.setVisibility(View.GONE);
            } else {
                llFilter.setVisibility(View.VISIBLE);
                type = null;
                btDeal.setTextColor(getResources().getColor(R.color.lib_dominant));
                btInvoke.setTextColor(getResources().getColor(R.color.libTextGray));
                btDeal.setBackgroundResource(R.drawable.bibi_shape);
                btInvoke.setBackgroundResource(R.drawable.bibi_shape1);
                if (viewPager.getCurrentItem() == 0) {
                    btDeal.setText(App.INSTANCE.getString(R.string.bibitipBibiUtil1));
                    btInvoke.setText(App.INSTANCE.getString(R.string.bibirecordActivity5));
                } else {
                    btDeal.setText(App.INSTANCE.getString(R.string.bibirecordActivity6));
                    btInvoke.setText(App.INSTANCE.getString(R.string.bibirecordActivity7));
                }
                if (leftSpinner != null) {
                    leftSpinner.setSelectIndex(0);
                    rightSpinner.setSelectIndex(0);
                }
            }
        });

        ClickUtil.click(btCancel, () -> {
            llFilter.setVisibility(View.GONE);
        });
        ClickUtil.click(view, () -> {
            llFilter.setVisibility(View.GONE);
        });
        ClickUtil.click(btDeal, () -> {
            if ("1".equals(type)) {
                btDeal.setTextColor(getResources().getColor(R.color.libTextGray));
                type = null;

                return;
            }
            type = "1";
            btDeal.setBackgroundResource(R.drawable.bibi_shape);
            btInvoke.setBackgroundResource(R.drawable.bibi_shape1);
            btDeal.setTextColor(getResources().getColor(R.color.lib_dominant));
            btInvoke.setTextColor(getResources().getColor(R.color.libTextGray));
        });
        ClickUtil.click(btInvoke, () -> {
            if ("2".equals(type)) {
                btInvoke.setTextColor(getResources().getColor(R.color.libTextGray));
                type = null;
                return;
            }
            type = "2";
            btDeal.setBackgroundResource(R.drawable.bibi_shape1);
            btInvoke.setBackgroundResource(R.drawable.bibi_shape);
            btDeal.setTextColor(getResources().getColor(R.color.libTextGray));
            btInvoke.setTextColor(getResources().getColor(R.color.lib_dominant));
        });
        ClickUtil.click(btConfirm, () -> {
            String code;
            if (rightSpinner.getSelectIndex() == 0) {
                code = null;
            } else {
                code = rightCodes.get(rightSpinner.getSelectIndex()) + "/" + leftCodes.get(leftSpinner.getSelectIndex());
            }
            if (viewPager.getCurrentItem() == 0) {
                RxBus.getDefault().post(new AllEntrustBusBean(code, type));
            } else {
                RxBus.getDefault().post(new HistoryEntrustBusBean(code, type));
            }
            llFilter.setVisibility(View.GONE);

        });
    }

    private void changeLeftSpinnerCode() {
        if (currentRightCode == null) {
            ArrayList<String> strings = new ArrayList<>();
            strings.add(App.INSTANCE.getString(R.string.bibirecordActivity8));
            leftCodes = strings;
            leftSpinner.setItemGravity(Gravity.CENTER);
            leftSpinner.attachData(leftCodes);
            return;
        }
        leftCodes = coinMap.get(currentRightCode);
        leftSpinner.setItemGravity(Gravity.CENTER);
        leftSpinner.attachData(leftCodes);

    }
}
